<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div class="easyui-layout" fit="true">
  <div region="center" style="padding:0px;border:0px;" split="true">
  <t:datagrid name="productList" title="" pagination="false"  onClick="mallShopCardruleAddModule.rowclick" actionUrl="mallShopCardruleController.do?productdatagrid&type=1&shopid=${shopid}" idField="id" fit="true">
   <t:dgCol title="商品id" field="id"    width="120"></t:dgCol>
   <t:dgCol title="商品名称" field="productname"   width="120"></t:dgCol>
   <t:dgCol title="商品编码" field="productcode"   width="120"></t:dgCol>
   <t:dgCol title="创建时间" field="createtime" formatter="yyyy-MM-dd"  width="120"></t:dgCol>
  </t:datagrid>
  </div>
 </div>
<div region="south"  style="height: 300px" split="true">
    <input type="hidden" id="curUserId" value="${curUserId}"/>
    <input type="hidden" id="curProjectId" value="${curProjectId}"/>
    <form id="mallShopCardrule" action="mallShopCardruleController.do?edit">
        <input type="hidden" value="${shopid}" name="shopid" id="eshopid">
        <input type="hidden" value="${cardlist}" name="cardno" id="ecardno">
        <input type="hidden" id="product_id" name="productid" value="${productid}">
        <input type="hidden" id="prefix" name="cardNoPrefix">
        <table style="width: 600px;" cellpadding="0" cellspacing="1" class="formtable">
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        店铺:
                    </label>
                </td>
                <td class="value">
                   <span>${shopname}</span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        商品:
                    </label>
                </td>
                <td class="value">
                   <span id="product_name" >${productname}</span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        供应商:
                    </label>
                </td>
                <td class="value">
                   <select name="oilsupplierid" datatype="*" id="eoilsupplierid">
                       <c:forEach items="${malloilsuppliers}" var="malloilsuppliers">
                           <option value="${malloilsuppliers.id}"
                                   <c:if test="${curOil==malloilsuppliers.id}">
                                       selected="selected"
                            </c:if>>
                                   ${malloilsuppliers.suppliername}
                           </option>
                       </c:forEach>
                   </select>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        关联项目:
                    </label>
                </td>
                <td class="value">
                    <select name="projectId" datatype="*" id="projectId">
                    </select>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label">
                        关联人员:
                    </label>
                </td>
                <td class="value">
                    <select name="projectUserId" datatype="*" id="projectUserId">
                    </select>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <label class="Validform_label" >
                        卡号:
                    </label>
                </td>
                <td class="value">
                    <span>${cardlist}</span>
                </td>
            </tr>
            <tr>
                <td align="right">
                </td>
                <td class="value">
                    <input type="button" value="确定" id="edit">
                    <%--<input type="button" value="取消" id="cancel" onclick="closeEditView()">--%>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
        </table>
    </form>
</div>


<script type="text/javascript">
    function closeEditView() {
        window.top.closeTab('tab_editview');
    }
    function projectUser(val){
        if(val==="-1"){
            $("#projectUserId").html("<option value='-1'>无</option>");
        }else {
            $.post("mallProjectController.do?projectEmpList", {projectId: val,isPage:false}, function (data) {
                var result = JSON.parse(data);
                var res = result.rows;
                var projects = "<option value='-1'>无</option>";
                if(result.success) {
                    for (var i=0;i<res.length;i++) {
                        var o = res[i];
                        projects += "<option value='" + o.employeeId + "'>" + o.realName + "</option>";
                    }
                }
                $("#projectUserId").html(projects);
            });
        }
    }
    function projects(){
        $.post("mallProjectController.do?projectList",{isPage:false},function (data) {
            var result = JSON.parse(data);
            var res = result.rows;
            var projects = "<option value='-1'>无</option>";
            if(result.success) {
                for (var i=0;i<res.length;i++) {
                    var o = res[i];
                    projects += "<option value='" + o.id + "'>" + o.projectname + "</option>";
                }
            }
            $("#projectId").html(projects);
            projectUser("-1");
        });
    }
    $(function() {
       // mallShopCardruleAddModule.init();
        projects();
        $("#projectId").change(function(){
            var val = $("#projectId").val();
            projectUser(val);
        });
        $("#edit").click(function(){
            var shopId = $("#eshopid").val();
            var cardNos = $("#ecardno").val();
            var product_id = $("#product_id").val();
            var eoilsupplierid = $("#eoilsupplierid").val();
            var projectId = $("#projectId").val();
            var projectUserId = $("#projectUserId").val();
            if(projectUserId=="-1" && projectId!="-1") {
                $.messager.alert('错误', "关联人员不能为空");
                return;
            }
            var data={shopid:shopId,cardno:cardNos,productid:product_id,oilsupplierid:eoilsupplierid,projectUserId:projectUserId}
            $.post("mallShopCardruleController.do?edit",data,function(res){
                var data=JSON.parse(res);
                if(data.success==true){
                    window.document.location.href  = 'mallShopCardruleController.do?list'
                }else {
                    alert(res);
                }
            });
        });
        var curUserId = $("#curUserId").val();
        var curProjectId = $("#curProjectId").val();
        setTimeout(function () {

            if(curProjectId != ''){

                $("#projectId option[value="+curProjectId+"]").attr("selected","selected");
                projectUser(curProjectId);
                setTimeout(function(){
                    $("#projectUserId option[value="+curUserId+"]").attr("selected","selected");
                },300);
            }
        },100);

    });
    var mallShopCardruleAddModule = (function(){
        //表单验证
        var fromValidform = function(){
            $("#mallShopCardrule").Validform({
                tiptype:4,
                btnSubmit:"#save",
                ajaxPost:true,
                datatype:{
                    //油卡验证
                    "cardrule":function(gets,obj,curform,regxp){

                      return true;
                    }
                },
                beforeSubmit:function(){
                    console.log($("#mallShopCardrule").serialize());
                    return true;
                },
                callback:function(data){
                    if(data.success==true){
                        $.messager.alert('成功',data.msg,"",function(){
                            window.document.location.href  = 'mallShopCardruleController.do?list'
                        });
                    }else{
                        $.messager.alert('错误',data.msg);
                    }
                }
            });
        }


        var module = {
            init :function(){
                fromValidform();
            },
           rowclick:function(rowIndex, rowData){
                var productId = rowData.id;
                var productName = rowData.productname;
                $("#product_id").val(productId);
                $("#product_name").text(productName);
               fromValidform();
            }
        }
        return module;
    })();
</script>